package org.system.core.domain.model;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author BizBin
 * @Date 2025/1/21 17:14
 * @Description
 */
@Component
@Primary
public class DataSourceModel extends DataSourceProperties{

    private static final Logger logger =  LoggerFactory.getLogger(DataSourceModel.class);

    private String databaseName;

    private String ipAddr;

    private String port;

    @PostConstruct
    public void init() {
        try {
            String databaseUrl = getUrl();
            String regex = "://([^:]+):(\\d+)/([^/?]+)";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(databaseUrl);
            if (matcher.find()) {
                setIpAddr(matcher.group(1));
                setPort(matcher.group(2));
                setDatabaseName(matcher.group(3));
                logger.info("Parsing success ===> id: {}, port: {}, name: {}", getIpAddr(), getPort(), getDatabaseName());
            }
        } catch (Exception e) {
            logger.info("解析失败, {}", e.getMessage());
        }
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
